<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="./style.css">

    <!-- Yandex Games SDK -->
    <script src="/sdk.js"></script>

    <style>
        canvas:focus {
            outline: none;
        }

        html, body {
            padding: 0;
            margin: 0;
            overflow: hidden;
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            -webkit-tap-highlight-color: rgba(0,0,0,0);
            height: 100%;
        }
    </style>

    <!-- Additional head modules -->

</head>
<body class="{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}">

    <div id="unity-container" class="unity-desktop">
        <canvas id="unity-canvas" tabindex="-1"></canvas>
    </div>
    <div id="loading-cover" style="display:none;">
        <div id="unity-loading-bar">
            <div id="unity-logo"><img src="Images/logo.png"></div>
            <div id="unity-progress-bar-empty" style="display: none;">
                <div id="unity-progress-bar-full"></div>
            </div>
            <div class="spinner"></div>
        </div>
    </div>

    <!-- Additional body modules -->

    <script>
        const hideFullScreenButton = "";
        const buildUrl = "Build";
        const loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
        const config = {
            dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
            frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
            codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
            #if MEMORY_FILENAME
        memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
            #endif
        #if SYMBOLS_FILENAME
        symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
            #endif
        streamingAssetsUrl: "StreamingAssets",
            companyName: "{{{ COMPANY_NAME }}}",
            productName: "{{{ PRODUCT_NAME }}}",
            productVersion: "{{{ PRODUCT_VERSION }}}"
        };

        const container = document.querySelector("#unity-container");
        const canvas = document.querySelector("#unity-canvas");
        const loadingCover = document.querySelector("#loading-cover");
        const progressBarEmpty = document.querySelector("#unity-progress-bar-empty");
        const progressBarFull = document.querySelector("#unity-progress-bar-full");
        const spinner = document.querySelector('.spinner');

        const canFullscreen = (function () {
            for (const key of [
                'exitFullscreen',
                'webkitExitFullscreen',
                'webkitCancelFullScreen',
                'mozCancelFullScreen',
                'msExitFullscreen',
            ]) {
                if (key in document) {
                    return true;
                }
            }
            return false;
        }());

        if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
            container.className = "unity-mobile";
            config.devicePixelRatio = 1;
        }

        // The background image when loading the game. When building a build, the code changes depending on the project settings.
#if BACKGROUND_FILENAME
        var backgroundUnity = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
#endif
        loadingCover.style.background = "url('Images/background.png') center / cover";
        loadingCover.style.display = "";

        let StartUnityInstance;
        let ygGameInstance = null;
        let ysdk = null;
        let initYSDK = false;
        let initGame = false;
        let syncInit = false;
        const NO_DATA = 'no data';

        document.addEventListener('contextmenu', event => event.preventDefault());
        function FocusGame() {
            window.focus();
            canvas.focus();
        }
        window.addEventListener('pointerdown', () => {
            FocusGame();
        });
        window.addEventListener('touchstart', () => {
            FocusGame();
        });

        document.addEventListener('visibilitychange', function () {
            if (ysdk !== null && initGame == true) {
                if (document.hidden) {
                    YG2Instance('SetFocusWindowGame', 'false');
                }
                else {
                    YG2Instance('SetFocusWindowGame', 'true');

                    if ((ysdk.deviceInfo.isMobile() || ysdk.deviceInfo.isTablet()) && ysdk.screen.fullscreen.status == 'off')
                        ysdk.screen.fullscreen.request();
                }
            }
        });

        const script = document.createElement("script");
        script.src = loaderUrl;
        script.onload = () => {
            StartUnityInstance = function () {
                createUnityInstance(canvas, config, (progress) => {
                    spinner.style.display = "none";
                    progressBarEmpty.style.display = "";
                    const adjustedProgress = Math.max(progress, 0.05);
                    progressBarFull.style.width = `${100 * adjustedProgress}%`;
                }).then((unityInstance) => {
                    ygGameInstance = unityInstance;
                    loadingCover.style.display = "none";
                }).catch((message) => {
                    console.error(message);
                });
            };
            
            if (LocalHost() || syncInit)
                StartUnityInstance_IfUnloaded();
        };

        function StartUnityInstance_IfUnloaded() {
            if (spinner.style.display !== "none")
                StartUnityInstance();
        }

        function LocalHost() {
            var host = window.location.hostname;
            if (host == "localhost" || host == "127.0.0.1")
                return true;
            else return false;
        }

        function YG2Instance(method) {
            if (ygGameInstance == null)
                return;

            if (!initGame) {
                setTimeout(function () {
                    if (ygGameInstance)
                        ygGameInstance.SendMessage('YG2Instance', method);
                }, 100);
            }
            else {
                ygGameInstance.SendMessage('YG2Instance', method);
            }
                
        }
        function YG2Instance(method, arg) {
            if (ygGameInstance == null)
                return;

            if (!initGame) {
                setTimeout(function () {
                    ygGameInstance.SendMessage('YG2Instance', method, arg);
                }, 100);
            }
            else {
                ygGameInstance.SendMessage('YG2Instance', method, arg);
            }
        }

        function LogStyledMessage(message, style) {
            console.log('%c' + message, style);
        }
        function LogStyledMessage(message) {
            console.log('%c' + message, 'color: #FFDF73; background-color: #454545');
        }

        InitYSDK();
        async function InitYSDK() {
            try {
                if (LocalHost()) return;

                ysdk = await YaGames.init();

                // Additional init0 modules

                // Additional init1 modules

                // Additional init2 modules

                // Additional init modules

                initYSDK = true;
                if (ygGameInstance != null)
                    ygGameInstance.SendMessage('YG2Instance', 'InitSDKComplete');

                LogStyledMessage('Init YandexSDK Success');

            } catch (e) {
                console.error('CRASH Initialization SDK: ', e);
            }

            if (!LocalHost() && !syncInit)
                StartUnityInstance_IfUnloaded();
        }

        function InitGame() {
            initGame = true;

            // Additional start modules
        }

        // Additional script modules

        document.body.appendChild(script);
    </script>
</body>
</html>
